﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNH_DAL_WS
{
    public class CatalogDAO
    {
        //lay danh sach cac catalog
        public List<CatalogDTO> GetAllCatalog()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var results = from catalog in data.CATALOGs
                          where catalog.IsDelete == false
                          select catalog;

            List<CatalogDTO> retVal = new List<CatalogDTO>();
            foreach (CATALOG catalog in results)
            {
                retVal.Add(new CatalogDTO(catalog.ID, catalog.Name));
            }

            return retVal;
        }

        //lay danh sach chi tiet cac cactalog
        public List<CatalogDetailDTO> GetAllCatalogDetail()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from cat in data.CATALOGs
                         where cat.IsDelete == false
                         select new CatalogDetailDTO(cat.ID, cat.Name, cat.MEALs.Count(e => e.IsDelete == false));

            List<CatalogDetailDTO> retVal = result.ToList<CatalogDetailDTO>();
            return retVal;
        }

        //them moi catalog
        public bool AddCatalog(CatalogDTO catalogDTO)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var check = from cat in data.CATALOGs
                        where cat.Name.Equals(catalogDTO.Name) && cat.IsDelete == false
                        select cat;
            if (check.Count() > 0)
            {
                return false;
            }

            //lay ma so cuoi cung
            var lastID = from cat in data.CATALOGs
                         orderby cat.ID descending
                         select cat.ID;

            //lay ma prefix
            var catCode = (from prefix in data.CODEs
                           where prefix.Data.Equals("catalog")
                           select prefix).First();

            string catID = catCode.Prefix;

            if (lastID.Count() > 0)
            {
                string tmp = lastID.First();
                catID = Ultilities.GetNextId(tmp, "catalog");
            }
            else
            {
                for (int i = 0; i < Int32.Parse(catCode.PostfixLength.ToString()); i++)
                {
                    catID += "0";
                }
            }

            catalogDTO.ID = catID;

            try
            {
                CATALOG catalog = new CATALOG();
                catalog.ID = catalogDTO.ID;
                catalog.Name = catalogDTO.Name;
                catalog.IsDelete = false;

                data.CATALOGs.InsertOnSubmit(catalog);
                data.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        //chinh sua catalog
        public bool EditCatalog(CatalogDetailDTO catalogDetail)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();
            var catalog = from cat in data.CATALOGs
                           where cat.ID.Equals(catalogDetail.ID)
                           select cat;

            if (catalog.Count() > 0)
            {
                catalog.First().Name = catalogDetail.Name;
                catalog.First().IsDelete = false;

                try
                {
                    data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }

        //xoa catalog
        public bool RemoveCatalog(string catalogID)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from cat in data.CATALOGs
                         where cat.ID.Equals(catalogID) && cat.IsDelete == false
                         select cat;

            if (result.Count() <= 0)
                return false;

            CATALOG catalog = result.First();

            try
            {
                catalog.IsDelete = true;

                foreach (MEAL meal in catalog.MEALs)
                {
                    meal.IsDelete = true;
                }

                data.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}